﻿//source code from Microsoft Mango Training Kit
//Comment by Alexis 2011-08-13

using System;
using System.Linq;
using Microsoft.Phone.Shell;

namespace AccountHelper.Common
{
    public static class ShellTileHelper
    {
        /// <summary>
        /// 当前Uri是否已经Pin到桌面
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static bool IsPinned(Uri uri)
        {
            var item = ShellTile.ActiveTiles.FirstOrDefault
                (x => x.NavigationUri == uri);

            return item != null;
        }

        /// <summary>
        /// 当前id是否已经Pin到桌面
        /// </summary>
        /// <param name="uniqueId"></param>
        /// <returns></returns>
        public static bool IsPinned(string uniqueId)
        {
            var item = ShellTile.ActiveTiles.FirstOrDefault
                (x => x.NavigationUri.ToString().Contains(uniqueId));

            return item != null;
        }

        /// <summary>
        /// 将瓷砖Pin到桌面
        /// </summary>
        /// <param name="uri">指定的url，可以带参数</param>
        /// <param name="initialData"></param>
        public static void Pin(Uri uri, ShellTileData initialData)
        {
            // Create the tile and pin to start. This will cause the app to be deactivated
            ShellTile.Create(uri, initialData);

        }

        /// <summary>
        /// 将瓷砖从桌面移除
        /// </summary>
        /// <param name="id">指定的id</param>
        public static void UnPin(string id)
        {
            var item = ShellTile.ActiveTiles.FirstOrDefault
                        (x => x.NavigationUri.ToString().Contains(id));

            if (item != null)
                item.Delete();
        }

        /// <summary>
        /// 将瓷砖从桌面移除
        /// </summary>
        /// <param name="uri">指定的Url</param>
        public static void UnPin(Uri uri)
        {
            var item = ShellTile.ActiveTiles.FirstOrDefault
                (x => x.NavigationUri == uri);

            if (item != null)
                item.Delete();
        }
    }
}
